<template>
  <div>

    <q-layout>
      <q-layout-header ref="header">
        <xheader :title="title"></xheader>
      </q-layout-header>
      <q-page-container>
        <q-page padding>
          <q-input ref="code" float-label="配方清单号" v-model="code" clearable
                   @blur="searchBatchInfo"
                   @keydown.enter="searchBatchInfo" :after="$parent.getScanIcon('code',this)"/>
          <q-input float-label="配方信息" placeholder="请输入配方清单号" readonly v-model="codeInfo" type="textarea" rows="3"
                   hide-underline/>
          <q-input ref="batchNo" float-label="物料批次条码" v-model="batchNo" clearable
                   @blur="getMaterialInfo" :after="$parent.getScanIcon('batchNo',this)"
                   @keydown.enter="getMaterialInfo"/>
          <br>
          <q-btn label="提交" color="primary" class="full-width" @click="submit"/>
          <br><br>
          <table>
            <thead>
            <tr>
              <th>物料</th>
              <th>供应商</th>
              <th>批次</th>
              <th>领用量</th>
              <th>扫描次数</th>
            </tr>
            </thead>
            <tbody>
            <tr v-for="p in wlBatchInfo">
              <td>{{p.batchNo}}</td>
              <td>{{p.verdorName}}</td>
              <td>{{p.batchNo}}</td>
              <td>
                <x-input v-model="p.qty" type="number"><span slot="right">{{p.uomName}}</span>
                </x-input>
              </td>
              <td>{{p.addCnt}}</td>
            </tr>
            </tbody>
          </table>
        </q-page>
      </q-page-container>
    </q-layout>

  </div>
</template>

<script>



import utils from '../../js/utils'
import Xheader from '../../components/xheader'

export default {
  name: 'pei_maiterial',
  components: { Xheader },
  props: {
    title: {}
  },
  data: () => ({
    code: '',
    codeInfo: '',
    batchNo: '',
    batchId: '',
    wlBatchInfo: ''
  }),
  methods: {
    submit () {
      if (!this.batchId) {
        this.$parent.info('请先扫描配方单！')
        return
      }
      let data = []
      for (let no in this.wlBatchInfo) {
        data.push(this.wlBatchInfo[no])
      }
      if (data.length === 0) {
        this.$parent.info('请扫描配物料批次号！')
        return
      }
      let flag = false
      for (let v of data) {
        if (!(v.qty * 1 >= 0)) {
          flag = true
        }
      }
      if (flag) {
        this.$parent.info('[领用量]请输入大于等于0的数！')
        return
      }
      this.$parent.confirm('提示', '确认提交？', () => {
        this.axios.post(this.$conf.server + '/manuPeifang/subPeiMaterial', this.$qs.stringify({
          id: this.batchId,
          batchInfo: JSON.stringify(data)
        })).then(res => {
          this.$parent.info(res.data.msg)
          if (res.data.result) {
            this.batchId = ''
            this.code = ''
            this.batchNo = ''
            this.codeInfo = ''
            this.wlBatchInfo = ''
          }
        })
      })
    },
    searchBatchInfo () {
      if (this.code !== '') {
        this.wlBatchInfo = {}

        this.axios.post(this.$conf.server + '/manuPeifang/getPeifangInfo', this.$qs.stringify({
          code: this.code,
          codeType: 'B'
        })).then(res => {
          if (res.data.result) {
            this.batchId = res.data.ID
            this.codeInfo = '规格型号：' + utils.nvl(res.data.CUT_METHOD) +
                '\r\n类型：' + utils.nvl(res.data.TYPENAME) +
                '\r\n配方名称：' + utils.nvl(res.data.NAME) + '(' + utils.nvl(res.data.VERSION) + ')'
          } else {
            this.$parent.info(res.data.msg)
          }
        })

        this.axios.post(this.$conf.server + '/manuPeifang/getPeifangInList', this.$qs.stringify({ code: this.code })).then(res => {
          if (res.data.result) {
            let list = res.data.list
            if (list) {
              for (let i of list) {
                this.wlBatchInfo[i.BATCHNO] = {
                  batchNo: i.BATCHNO,
                  qty: i.QTY * 1,
                  addCnt: 1
                }
              }
            }
          } else {
            this.$parent.info(res.data.msg)
          }
        })
      }
    },
    getMaterialInfo () {
      if (this.batchNo !== '') {
        if (this.wlBatchInfo[this.batchNo] && this.wlBatchInfo[this.batchNo].weight) {
          this.wlBatchInfo[this.batchNo].qty += this.wlBatchInfo[this.batchNo].weight
          this.wlBatchInfo[this.batchNo].addCnt += 1
          return
        }
        this.axios.post(this.$conf.server + '/useMaterial/getMaterialInfo', this.$qs.stringify({
          batchNo: this.batchNo,
          batchId: this.batchId
        })).then(res => {
          if (res.data.result) {
            if (this.wlBatchInfo[this.batchNo]) {
              this.wlBatchInfo[this.batchNo].weight = res.data.WEIGHT
              this.wlBatchInfo[this.batchNo].qty += this.wlBatchInfo[this.batchNo].weight
              this.wlBatchInfo[this.batchNo].addCnt += 1
              this.wlBatchInfo[this.batchNo].verdorName = res.data.UOMNAME
            } else {
              if (res.data.noPeifang) {
                this.$parent.confirm('提示', '此批次号物料[' + res.data.PNAME + ']不在配方清单中，确定添加此物料？',
                  () => {
                    this.wlBatchInfo[this.batchNo] = {
                      batchNo: this.batchNo,
                      weight: res.data.WEIGHT,
                      qty: res.data.WEIGHT,
                      addCnt: 1 // 扫描次数
                    }
                  })
              }
            }
          } else {
            this.$parent.info(res.data.msg)
          }
        })
      }
    }
  }
}
</script>

<style scoped>
  table {
    border-collapse: collapse;
    margin: 0 auto;
    text-align: center;
  }

  table td, table th {
    border: 1px solid #cad9ea;
    color: #666;
    height: 30px;
  }

  table thead th {
    background-color: #CCE8EB;
    width: 100px;
  }

  table tr:nth-child(odd) {
    background: #fff;
  }

  table tr:nth-child(even) {
    background: #F5FAFA;
  }
</style>
